<?php
use Workerman\Worker;
use Workerman\Channel\Client;
require_once '/Workerman/Autoloader.php';

// 用来处理http请求，向任意客户端推送数据，需要传workerID和connectionID
$http_worker = new Worker('http://0.0.0.0:4237');
$http_worker->name = 'publisher';

$http_worker->onWorkerStart = function() {
    Client::connect('sub.nao.com', 2206);
};

$http_worker->onMessage = function($connection, $data) {
	$connection->send('ok');

    if(empty($_POST)) return;

    $data = array('data' => json_encode($_POST));
    $event_name = '广播';

    // 是向某个worker进程中某个连接推送数据
    if(isset($_GET['to_worker_id']) && isset($_GET['to_connection_id'])) {
        $event_name = $_GET['to_worker_id'];
        $data['to_connection_id'] = $_GET['to_connection_id'];
    }

    echo "转发($event_name): ", var_export($data, true), PHP_EOL;
    Client::publish($event_name, $data);
};

Worker::runAll();
